import { Util, Rectangle } from '@antv/x6';
import { getSelectedEdges } from "../utils/flowChartUtils";
export function findViewsFromPoint(flowGraph, x, y) {
  return flowGraph.getCells().map(function (cell) {
    return flowGraph.findViewByCell(cell);
  }).filter(function (view) {
    if (view !== null) {
      var bBox = Util.getBBox(view.container, {
        target: flowGraph.view.stage
      });
      if (bBox.height < 16) {
        bBox = Rectangle.create({
          x: bBox.x,
          y: bBox.y - 8 + bBox.height / 2,
          width: bBox.width,
          height: 16
        });
      }
      return bBox.containsPoint({
        x: x,
        y: y
      });
    }
    return false;
  });
}
var registerEvents = function registerEvents(flowGraph) {
  flowGraph.on('selection:changed', function () {
    flowGraph.trigger('toolBar:forceUpdate');
    flowGraph.trigger('settingBar:forceUpdate');
  });
  flowGraph.on('edge:selected', function (args) {
    args.edge.attr('line/stroke', '#feb663', {
      ignore: true
    });
  });
  flowGraph.on('edge:unselected', function (args) {
    args.edge.attr('line/stroke', '#c1c1c1', {
      ignore: true
    });
  });
  flowGraph.on('edge:mouseover', function (args) {
    args.edge.attr('line/stroke', '#feb663', {
      ignore: true
    });
  });
  flowGraph.on('edge:mouseleave', function (args) {
    var edge = args.edge;
    var selectedEdges = getSelectedEdges(flowGraph);
    if (selectedEdges[0] !== edge) {
      args.edge.attr('line/stroke', '#c1c1c1', {
        ignore: true
      });
    }
  });
  flowGraph.on('node:dblclick', function () {
    flowGraph.trigger('graph:editNode');
  });
  flowGraph.on('blank:contextmenu', function (args) {
    var _args$e = args.e,
      clientX = _args$e.clientX,
      clientY = _args$e.clientY;
    flowGraph.cleanSelection();
    flowGraph.trigger('graph:showContextMenu', {
      x: clientX,
      y: clientY,
      scene: 'blank'
    });
  });
  flowGraph.on('node:contextmenu', function (args) {
    var _args$e2 = args.e,
      clientX = _args$e2.clientX,
      clientY = _args$e2.clientY,
      node = args.node;
    // NOTE: if the clicked node is not in the selected nodes, then clear selection
    if (!flowGraph.getSelectedCells().includes(node)) {
      flowGraph.cleanSelection();
      flowGraph.select(node);
    }
    flowGraph.trigger('graph:showContextMenu', {
      x: clientX,
      y: clientY,
      scene: 'node'
    });
  });
  flowGraph.on('blank:mousedown', function () {
    flowGraph.cleanSelection();
  });
  flowGraph.on('node:click', function (args) {
    var node = args.node;
    var model = node.getData().model;
    if (model) {
      flowGraph.trigger('model:select', model);
    }
  });
};
export default registerEvents;
